package com.fjnu.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.fjnu.dao.IenclosureInsDao;
import com.fjnu.dao.common.BaseDaoImpl;
import com.fjnu.dao.common.DaoException;
import com.fjnu.entities.EnclosureInstrument;
@Repository
public class EncInstrumentDao extends BaseDaoImpl<EnclosureInstrument> implements IenclosureInsDao {

	@Override
	public void addEncInstrument(EnclosureInstrument enclosureInstrument) {
		save(enclosureInstrument);

	}

	@Override
	public void delEncInstrument(int id) {
		delete(id);
	}

	@Override
	public List<EnclosureInstrument> findManyEncInstrument(String condition, int pageSize, int pageIndex) {
		String hql ="from EnclosureInstrument";
		//hql+="limit ?,?";
		//查找框根据查询条件查询数据
		if(condition!=null && !"".equals(condition)){
			hql+=" where enclosureName like '%"+condition+"%' or enclosureIdentifier like '%"+condition+"%'";
		}
		Query query = getSession().createQuery(hql);
		query.setFirstResult((pageIndex-1)*pageSize);
	    query.setMaxResults(pageSize);
	    //List list = find(pageIndex,pageSize,hql);
	    if(query.list().size()==0){
	    	throw new DaoException("找不到所需的附件设备");
	    }
		return query.list();
	}

	@Override
	public EnclosureInstrument findUniqueEncInstrument(int id) {
		
		return findById(id);
	}

	@Override
	public void updateEncInstrument(EnclosureInstrument enclosureInstrument) {
		update(enclosureInstrument);

	}

	@Override
	public void delManyEncIns(List listId) {
		deleteMany(listId);
	}

	/**
	 * 根据主件id查找其下的附件信息
	 */
	public List<EnclosureInstrument> findByMainId(int mainId) {
		// TODO Auto-generated method stub
		String hql="from EnclosureInstrument e where e.mainInstrument.id = ?";
		
		List<EnclosureInstrument> list =findByHQL(hql,mainId);
		if(list.size()==0){
			throw new DaoException("附件信息为空");
		}
		return list;
	}

}
